/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 * 						Sébastien Dufay
 * 						Romain Noel
 * 						Nicolas Dennewald
 * 						Slim Aloui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.esiee.mobiliteam;

import java.util.Date;
import java.util.Locale;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TabHost;

/**
 * Generation of the tabview by activity.
 * 
 * @author Mathieu Marleix <mathieu.marleix@aerian.fr>
 * @author Sébastien Dufay
 * @author Romain Noel
 * @author Nicolas Dennewald
 * @author Slim Aloui
 *
 */
public class TabWidget extends TabActivity{

	public Locale locale = null;
	private SharedPreferences sharedPrefs;
	public boolean DEBUG = true;
	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		if (locale != null)
		{
			newConfig.locale = locale;
			Locale.setDefault(locale);
			getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
		}
		launchTabWidget();
	}
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		launchTabWidget();
		sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		changeLocales();
	}

	/**
	 * Permet de changer la langue de l'application independamment de
	 * la locale de la machine.
	 */
	public void changeLocales(){
		Configuration config = getBaseContext().getResources().getConfiguration();

		String lang = sharedPrefs.getString("languages", "");
		if(DEBUG) System.out.println(sharedPrefs.getString("languages", ""));
		if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
		{
			locale = new Locale(lang);
			Locale.setDefault(locale);
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		}
	}
	/**
	 * Create the design of the tab and link it to the
	 * specified activity.
	 */
	public void launchTabWidget(){
		setContentView(R.layout.main);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost();  // The activity TabHost
		TabHost.TabSpec spec;  // Resusable TabSpec for each tab
		Intent intent;  // Reusable Intent for each tab

		Bundle param = this.getIntent().getExtras();
		String activityChoice = (String) param.getString("activityChoice");
		
		// Create an Intent to launch an Activity for the tab (to be reused)
		if(activityChoice.equals("CalendarActivity")){
			intent = new Intent().setClass(this, CalendarActivity.class);
			intent.setAction(CalendarActivity.MIME_TYPE);
			Bundle bundle2 = new Bundle();
			Date date = new Date();
			date.setDate(date.getDate());
			bundle2.putInt("day", date.getDate());
			bundle2.putInt("month", date.getMonth());
			bundle2.putBoolean("alarmChanged", this.getIntent().getExtras().getBoolean("alarmChanged",false));
			intent.putExtras(bundle2);
		}
		else if(activityChoice.equals("RssActivity"))
			intent = new Intent().setClass(this, RssActivity.class);
		else
			intent = new Intent().setClass(this, CalendarActivity.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("calendar").setIndicator(getString(R.string.calendar),
				res.getDrawable(R.drawable.ic_calendar))
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, MapMyCalendarActivity.class);
		spec = tabHost.newTabSpec("map").setIndicator(getString(R.string.map),
				res.getDrawable(R.drawable.ic_map))
				.setContent(intent);
		tabHost.addTab(spec);


		intent = new Intent().setClass(this, Transport.class);
		spec = tabHost.newTabSpec("transport").setIndicator(getString(R.string.transport),
				res.getDrawable(R.drawable.ic_transport))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
	}
	
}
